/*
 * @lc app=leetcode.cn id=1005 lang=javascript
 *
 * [1005] K 次取反后最大化的数组和
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var largestSumAfterKNegations = function (nums, k) {
  nums.sort((a, b) => a - b);
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] < 0 && k > 0) {
      nums[i] = -nums[i];
      k--;
      continue;
    }
    if (k === 0 || nums[i] === 0) { k = 0; break; }
    if (k % 2 === 1) {
      if (i === 0 || nums[i] < nums[i - 1]) {
        nums[i] = -nums[i];
      } else {
        nums[i - 1] = -nums[i - 1];
      }
      k = 0;
      break;
    }
  }
  if (k % 2 === 1) {
    nums[nums.length - 1] = -nums[nums.length - 1];
  }
  return nums.reduce((pre, cur) => pre + cur);
};
// @lc code=end

